
package time.basic;

import java.text.SimpleDateFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

/**
 * 資料來源為多天, 可以改變 X 軸的資料(第 69 行), 那麼 JFreeChart 會很聰明的把你 domain asix 的刻度隨著我們的資料來做變化 <br>
 * 
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2001-2008
 */

public class DaysTimeSeriesChart
{
	private String housePriceLengendLabel = "房價指數";

	private String title = "房價指數圖";

	private String timeAxisLabel = "估價日期";

	private String valueAxisLabel = "總價(萬元)";

	public DaysTimeSeriesChart()
	{
		JFreeChart chart = createTimeSeriesChart();

		XYPlot plot = ( XYPlot )chart.getPlot();

		DateAxis axis = ( DateAxis )plot.getDomainAxis();

		axis.setDateFormatOverride( new SimpleDateFormat( "yyyy-MM-dd" ) );

		XYItemRenderer r = plot.getRenderer();

		if( r instanceof XYLineAndShapeRenderer )
		{
			XYLineAndShapeRenderer renderer = ( XYLineAndShapeRenderer )r;
			renderer.setBaseShapesVisible( true );
			renderer.setBaseShapesFilled( true );
		}

		display( chart );
	}

	/**
	 * @param args
	 */
	public static void main( String[] args )
	{
		new DaysTimeSeriesChart();
	}

	private JFreeChart createTimeSeriesChart()
	{
		TimeSeries housePriceTS = new TimeSeries( housePriceLengendLabel, Day.class );
		housePriceTS.add( new Day( 25, 1, 2007 ), 521 );
		housePriceTS.add( new Day( 26, 1, 2008 ), 529 );
		housePriceTS.add( new Day( 27, 2, 2008 ), 523 );
		housePriceTS.add( new Day( 26, 3, 2008 ), 524 );
		housePriceTS.add( new Day( 27, 3, 2008 ), 522 );
		housePriceTS.add( new Day( 15, 4, 2008 ), 520 );
		housePriceTS.add( new Day( 20, 4, 2008 ), 529 );
		housePriceTS.add( new Day( 1, 5, 2008 ), 520 );
		housePriceTS.add( new Day( 2, 5, 2008 ), 500 );
		housePriceTS.add( new Day( 3, 5, 2008 ), 410 );
		housePriceTS.add( new Day( 4, 6, 2008 ), 470 );
		housePriceTS.add( new Day( 5, 6, 2008 ), 490 );
		housePriceTS.add( new Day( 6, 6, 2008 ), 550 );
		housePriceTS.add( new Day( 7, 6, 2008 ), 420 );
		housePriceTS.add( new Day( 8, 6, 2008 ), 522 );
		housePriceTS.add( new Day( 9, 6, 2008 ), 515 );

		TimeSeriesCollection dataset = new TimeSeriesCollection();
		dataset.addSeries( housePriceTS );

		return ChartFactory.createTimeSeriesChart( title, timeAxisLabel, valueAxisLabel, dataset, true, true, false );
	}

	private void display( JFreeChart chart )
	{
		ChartFrame frame = new ChartFrame( "Test", chart );
		frame.pack();
		frame.setVisible( true );
	}
}
